#pragma once

#ifdef WGLENGINE_DLL
#define WGLENGINE_EXPORT __declspec(dllexport)
#else
#define WGLENGINE_EXPORT __declspec(dllimport)
#endif

#include "glm\glm.hpp"
#include "WObject.h"

class WGLENGINE_EXPORT DirectionalLight : public WObject
{
private:
	glm::vec3 lookAt;
	glm::vec3 position;
	
	glm::vec3 forward;
	glm::vec3 right;
	glm::vec3 up;

protected:

	glm::mat4 matrix;

public:
	DirectionalLight()
	{
		lookAt = glm::vec3(0, 0, 0);
		position = glm::vec3(5, 5, 0);
	}

	~DirectionalLight()
	{

	}

	glm::vec3 GetForward()
	{
		return transform->GetForward();
	}

	glm::vec3 GetUp()
	{
		return transform->GetUp();
	}

	glm::vec3 GetRight()
	{
		return transform->GetRight();
	}

	glm::vec3 GetPosition()
	{
		return position;
	}

	virtual void PreUpdate() override
	{
		forward = lookAt - position;
		forward = glm::normalize(forward);

		if((forward.y > 0.9999f && forward.y < 1.00001f) || (forward.y < -0.9999f && forward.y > -1.0001f))
		{
			up = glm::vec3(1, 0, 0);
		}
		else
		{
			up = glm::vec3(0, 1, 0);
		}

		right = glm::cross(forward, up);
		right = glm::normalize(right);

		up = glm::cross(right, forward);
		up = glm::normalize(up);

		matrix[0] = glm::vec4(right.x, right.y, right.z, 0.0);
		matrix[1] = glm::vec4(up.x, up.y, up.z, 0.0);
		matrix[2] = glm::vec4(forward.x, forward.y, forward.z, 0.0);
		matrix[3] = glm::vec4(position.x, position.y, position.z, 0.0);

		transform->SetModelMatrix(matrix);
	}
};